package com.xh.bussiness.system.controller;

import com.xh.bussiness.course.bean.request.PrepareConfigGroupRequest;
import com.xh.bussiness.system.service.IGlobalPrepareConfigGroupService;
import com.xh.bussiness.system.model.GlobalPrepareConfigGroup;
import com.xh.bussiness.system.bean.request.GlobalPrepareConfigGroupRequest;
import com.xh.core.annotation.ExcludeLoginTokenIntercept;
import com.xh.core.bean.XhExportHeaderBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.xh.core.bean.AjaxResult;
import com.xh.core.bean.XhPage;
import com.xh.core.constant.AjaxResultConstant;
import org.springframework.validation.annotation.Validated;

import com.xh.core.controller.BaseController;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Jqm
 * @since 2021-02-05
 */
@RestController
@RequestMapping("/admin/globalPrepareConfigGroup")
public class GlobalPrepareConfigGroupController extends BaseController {

    @Autowired
    private IGlobalPrepareConfigGroupService globalPrepareConfigGroupService;

    /**
     * 分页数据查询
     * @param page 分页对象
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/queryPage")
    public XhPage queryPage(XhPage page, GlobalPrepareConfigGroupRequest bean){
        return globalPrepareConfigGroupService.queryGlobalPrepareConfigGroupPage(page,bean);
    }

    /**
     * 数据查询
     * @param bean 查询条件Bean
     * @return
     */
    @ExcludeLoginTokenIntercept
    @PostMapping("/queryAll")
    public AjaxResult query(GlobalPrepareConfigGroupRequest bean){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",globalPrepareConfigGroupService.queryGlobalPrepareConfigGroup(bean));
    }

    /**
     * 导出全部
     * @param bean
     * @return
     */
    @PostMapping(value="/exportAll")
    public AjaxResult exportAll(GlobalPrepareConfigGroupRequest bean, HttpServletRequest request) {
        try {
            LinkedHashMap<String,XhExportHeaderBean> exportHeaderBeans=new LinkedHashMap<>();
            //exportHeaderBeans.put("username",new XhExportHeaderBean("用户名"));
            //exportHeaderBeans.put("deleted",new XhExportHeaderBean("是否删除",ImmutableMap.of(0,"否",1,"是")));
            //exportHeaderBeans.put("createTime",new XhExportHeaderBean("创建时间","yyyy-MM-dd HH:mm:ss"));

            String downloadUrl=globalPrepareConfigGroupService.exportAll(request,exportHeaderBeans,bean,"queryGlobalPrepareConfigGroupPage");
            if(downloadUrl!=null){
                return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",downloadUrl);
            }else{
                return new AjaxResult(AjaxResultConstant.Result.ERROR,"导出失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(AjaxResultConstant.Result.ERROR);
        }
    }

    /**
     * 新增和保存
     * @param globalPrepareConfigGroup 实体对象
     * @return
     */
    @PostMapping(value="/saveOrUpdate")
    @Transactional
    public AjaxResult saveOrUpdate(@Validated GlobalPrepareConfigGroup globalPrepareConfigGroup){
        if(globalPrepareConfigGroup.getId()==null){
            globalPrepareConfigGroupService.insert(globalPrepareConfigGroup);
        }else{
            globalPrepareConfigGroupService.update(globalPrepareConfigGroup);
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 删除操作
     * @param groupId
     * @return
     */
    @PostMapping("/deleteById")
    @Transactional
    public AjaxResult deleteById(String groupId){
        // 直接物理删除
        globalPrepareConfigGroupService.deleteByGroupId(groupId);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 批量删除操作
     * @param ids
     * @return
     */
    @PostMapping("/batchDelete")
    @Transactional
    public AjaxResult batchDelete(String[] ids){
        globalPrepareConfigGroupService.batchDelete(ids);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 根据组id分页查询用户
     * @param page
     * @param groupId
     * @param userName
     * @return
     */
    @PostMapping(value="/queryUsersByGroupId")
    public XhPage queryUsersByGroupId(XhPage page, String groupId, String userName){
        return globalPrepareConfigGroupService.queryUsersByGroupId(page, groupId, userName);
    }

    /**
     * 添加用户
     * @param bean
     * @return
     */
    @PostMapping(value="/changeUserGroup")
    @Transactional
    public AjaxResult changeUserGroup(@RequestBody GlobalPrepareConfigGroupRequest bean){

        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,globalPrepareConfigGroupService.changeUserGroup(bean));
    }

    /**
     * 分页查询用户
     * @param page
     * @param courseId
     * @param userName
     * @return
     */
    @PostMapping(value="/queryUsers")
    @Transactional
    public AjaxResult queryUsers(XhPage page, String courseId, String userName){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,globalPrepareConfigGroupService.queryUsers(page, courseId, userName));
    }
}

